package fr.mairie.parc.control.materiel;

import java.util.ArrayList;
import java.util.List;

import org.apache.click.Page;
import org.apache.click.control.AbstractContainer;
import org.apache.click.control.AbstractLink;
import org.apache.click.control.Column;
import org.apache.click.control.PageLink;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.LinkDecorator;
import org.apache.click.extras.control.TableInlinePaginator;
import org.apache.click.util.Bindable;

import fr.mairie.parc.domain.Materiel;
import fr.mairie.parc.page.materiel.ViewMaterielPage;

public class MaterielTable extends AbstractContainer {

	private static final long serialVersionUID = 1L;
	private Object owner;

	@Bindable protected Table table = new Table("table");
	@Bindable protected PageLink actionRowLink;
	private Column actionColumn;
	private List<AbstractLink> actionColumnLinks = new ArrayList<AbstractLink>();

	public MaterielTable (String name, Object owner,
			Class<? extends Page> actionRowPageClass) {
		super(name);
		this.owner = owner;

		table.setClass(Table.CLASS_ITS);
		table.setPageSize(5);
		table.setShowBanner(true);
		table.setSortable(true);
		table.setPaginator(new TableInlinePaginator(table));
		table.setPaginatorAttachment(Table.PAGINATOR_INLINE);


		table.addColumn(new Column("idMateriel", "ID"));
		table.addColumn(new Column("type", "Type"));
		table.addColumn(new Column("marque", "Marque"));
		table.addColumn(new Column("modele", "Modéle"));
		table.addColumn(new Column("stock", "Stock"));
		table.addColumn(new Column("prix", "Prix"));
		table.addColumn(new Column("numParc", "Numéro parc"));
		table.addColumn(new Column("etat", "Etat"));

		this.actionColumn = new Column("Actions");
		actionColumn.setTextAlign("center");

		{
			PageLink viewRowLink = new PageLink("id", ViewMaterielPage.class);
			viewRowLink.setImageSrc("/assets/images/16x16/application_form_magnify.png");
			actionColumnLinks.add(viewRowLink);
		}

		if (actionRowPageClass != null && actionRowPageClass != ViewMaterielPage.class) {
			actionRowLink = new PageLink(actionRowPageClass);
			actionRowLink.setImageSrc("/assets/images/16x16/application_form_edit.png");
			actionColumnLinks.add(actionRowLink);
		}
		AbstractLink[] linksArray = actionColumnLinks.toArray(new AbstractLink[actionColumnLinks.size()]);
		actionColumn.setDecorator(new LinkDecorator(table, linksArray, "idMateriel"));

		actionColumn.setSortable(false);
		table.addColumn(actionColumn);

		add(table);
	}

	public Object getOwner() {
		return owner;
	}

	public void addActionColumnLink(AbstractLink link) {
		actionColumnLinks.add(link);
		updateActionColumnLinks();
	}

	protected void updateActionColumnLinks() {
		AbstractLink[] linksArray = actionColumnLinks.toArray(new AbstractLink[actionColumnLinks.size()]);
		actionColumn.setDecorator(new LinkDecorator(table, linksArray, "idMateriel"));
	}

	public void setDataProvider(DataProvider<Materiel> dataProvider) {
		table.setDataProvider(dataProvider);
	}

	public void setRowList(List<Materiel> m) {
		table.setRowList(m);
	}

	@SuppressWarnings("unchecked")
	public List<Materiel> getRowList() {
		return (List<Materiel>) table.getRowList();
	}


}
